/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aliasMatching.cluster.eightCluster;

import aliasMatching.model.Alias;
import aliasMatching.model.TestTableModel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import rdfparser.util.ExecutionTimeCalculate;

/**
 *
 * @author amendrashrestha
 */
public class TimeStyloClusterEqualityTest {

    List matrixUsers = new ArrayList();
    String matrix;
    List userPosts = new ArrayList();
    List commonPost = new ArrayList();
    List userPostsTime = new ArrayList();
    List firstUserPost, secondUserPost, firstUserPostTime, secondUserPostTime;
    String firstUser, secondUser;
    List userList;
    List clusterUser;
    TestTableModel mdl;
    JTable displayResult;
    static JScrollPane scrollPane;

    public TimeStyloClusterEqualityTest() {
        mdl = new TestTableModel();
    }

    void calculateTimeStyloEquality(HashMap<String, List> matrixMap) throws SQLException {
        JScrollPane createTestTable = new JScrollPane();
        for (Map.Entry<String, List> userEntry : matrixMap.entrySet()) {

            matrixUsers = userEntry.getValue();
            matrix = userEntry.getKey();
            clusterUser = new ArrayList();

            if (matrixUsers.size() > 1) {
                createTestTable = createTestTable(mdl, matrixUsers, matrix);
            }
        }

        JLabel test = new JLabel("Stylometric and Time Equaltiy after Clustering");
        test.setSize(200, 100);

        JFrame testFrame = new JFrame("Test");
        JPanel tablePanel = new JPanel();
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tablePanel.add(test, BorderLayout.NORTH);
        tablePanel.add(createTestTable, BorderLayout.CENTER);

        testFrame.add(tablePanel);
        testFrame.setSize(new Dimension(550, 700));
        testFrame.setVisible(true);
    }

    private JScrollPane createTestTable(TestTableModel mdl, List user, String matrix) throws SQLException {

        displayResult = new JTable(mdl);
        displayResult.setBackground(Color.white);
        displayResult.setAutoscrolls(true);
        displayResult.getTableHeader().setReorderingAllowed(false);
        scrollPane = new JScrollPane(displayResult);
        scrollPane.setPreferredSize(new Dimension(500, 640));
        displayResult.setFillsViewportHeight(true);
        getTimeStylo(user, mdl, matrix);

        return scrollPane;
    }

    public void getTimeStylo(List user, TestTableModel testMdl, String matrix) throws SQLException {

        List<Alias> styloUser = new ArrayList();

        TimeStyloCheck se = new TimeStyloCheck(user, null, testMdl, matrix);
        styloUser = se.aliases;
        int userSize = styloUser.size();
        Alias[] tempAlias = new Alias[userSize + 1];
        //Alias[] tempAlias_B = new Alias[userSize];

        String[] tempUser = new String[1];

        for (int i = 0; i < 1; i++) {
            tempUser[i] = styloUser.get(i).getUser();
            divideUser(tempUser[i]);

            styloUser.remove(0);

            tempAlias[i] = new Alias(firstUser);
            tempAlias[i].setPosts(firstUserPost);
            tempAlias[i].setPostTime(firstUserPostTime);
            styloUser.add(0, tempAlias[i]);

            tempAlias[i + 1] = new Alias(secondUser);
            tempAlias[i + 1].setPosts(secondUserPost);
            tempAlias[i + 1].setPostTime(secondUserPostTime);
            styloUser.add(tempAlias[i + 1]);
            //System.out.println("ALias A:" + tempAlias_B[i]);
        }
        for (int j = 1; j < styloUser.size() - 1; j++) {
            tempUser[0] = styloUser.get(j).getUser();
            String userID = tempUser[0].toString();

            styloUser.remove(j);

            tempAlias[j] = new Alias(userID);
            List post = Alias.getAllPosts(Integer.valueOf(userID));
            List postTimes = Alias.getAllPostTime(Integer.valueOf(userID));
            tempAlias[j].setPosts(post);
            tempAlias[j].setPostTime(postTimes);
            styloUser.add(j, tempAlias[j]);
        }

        /**
         * put _A user in first index and send to stylo after that remove that
         * user and again add another _A user from tempAlias_A list
         *
         */
        //for (int i = 0; i < userSize; i++) {
        long styloStartTime = System.currentTimeMillis();
        //styloUser.add(0, tempAlias_A[i]);
        se.executeStylo();
        //styloUser.remove(0);
        long styloEndTime = System.currentTimeMillis();
        System.out.println("Execution Time: " + ExecutionTimeCalculate.returnTime(styloStartTime, styloEndTime) + " min");
        System.out.println("----------------");
        //}
    }

    public void divideUser(String ID) throws SQLException {
        dividePost(ID);
        firstUser = ID + "_A";
        secondUser = ID + "_B";
    }

    /**
     * Dividing posts between users
     *
     * @param args
     * @throws SQLException
     */
    public void dividePost(String ID) throws SQLException {

        int tempID = Integer.parseInt(ID);
        userPosts = Alias.getAllPosts(tempID);
        userPostsTime = Alias.getAllPostTime(tempID);

        firstUserPost = new ArrayList();
        secondUserPost = new ArrayList();
        firstUserPostTime = new ArrayList();
        secondUserPostTime = new ArrayList();

        for (int i = 0; i < userPosts.size(); i++) {
//            String date = userPostsTime.get(i).toString();
//            int day = Integer.parseInt(date.substring(8, 10));
            /**
             * The odd number post are assigned to first split user and even
             * number are assigned to second split user.
             */
            if (i % 2 == 0) {
                firstUserPost.add(userPosts.get(i));
                firstUserPostTime.add(userPostsTime.get(i));
            } else {
                secondUserPost.add(userPosts.get(i));
                secondUserPostTime.add(userPostsTime.get(i));
            }
           /* if (day <= 7) {
            if (day % 2 != 0) {
            firstUserPostTime.add(userPostsTime.get(i));
            firstUserPost.add(userPosts.get(i));
            } else {
            secondUserPostTime.add(userPostsTime.get(i));
            secondUserPost.add(userPosts.get(i));
            }
            } if (day >= 8 && day <= 14) {
            if (day % 2 == 0) {
            firstUserPostTime.add(userPostsTime.get(i));
            firstUserPost.add(userPosts.get(i));
            } else {
            secondUserPostTime.add(userPostsTime.get(i));
            secondUserPost.add(userPosts.get(i));
            }
            }
            if(day >= 15 && day <= 21){
            if (day % 2 != 0) {
            firstUserPostTime.add(userPostsTime.get(i));
            firstUserPost.add(userPosts.get(i));
            } else {
            secondUserPostTime.add(userPostsTime.get(i));
            secondUserPost.add(userPosts.get(i));
            }
            }
            if(day >= 20 && day <= 26){
            if (day % 2 == 0) {
            firstUserPostTime.add(userPostsTime.get(i));
            firstUserPost.add(userPosts.get(i));
            } else {
            secondUserPostTime.add(userPostsTime.get(i));
            secondUserPost.add(userPosts.get(i));
            }
            }
            if(day >= 27 && day <= 31){
            if (day % 2 != 0) {
            firstUserPostTime.add(userPostsTime.get(i));
            firstUserPost.add(userPosts.get(i));
            } else {
            secondUserPostTime.add(userPostsTime.get(i));
            secondUserPost.add(userPosts.get(i));
            }
            }*/
        }
    }
}
